package org.class2table.export;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import org.class2table.core.Class2TableException;
import org.class2table.core.table.TableProcessor;
import org.class2table.core.table.bean.TableMetaData;
import org.class2table.core.util.ClassBuildUtil;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Objects;


public class ExportProcessor {

    private static List<String> exportTableList;

    private static String savePath;

    private static Connection originConnection;

    private static String originDbName;

    private static Map<String,Class<?>> dataType2ClazzRuleMap;

    public static void init(List<String> exportTableList, String savePath, Connection originConnection, String originDbName, Map<String,Class<?>> dataType2ClazzRuleMap) {
        if (CollectionUtil.isEmpty(exportTableList)) {
            throw new Class2TableException("ExportProcessor init failed! exportTableList is empty!");
        }
        if (StrUtil.isEmpty(savePath)) {
            throw new Class2TableException("ExportProcessor init failed! savePath is empty!");
        }
        if (Objects.isNull(originConnection)) {
            throw new Class2TableException("ExportProcessor init failed! originConnection is null!");
        }
        if (StrUtil.isEmpty(originDbName)) {
            throw new Class2TableException("ExportProcessor init failed! originDbName is empty!");
        }
        if (CollectionUtil.isEmpty(dataType2ClazzRuleMap)) {
            throw new Class2TableException("ExportProcessor init failed! dataType2ClazzRuleMap is empty!");
        }
        ExportProcessor.exportTableList = exportTableList;
        ExportProcessor.savePath = savePath;
        ExportProcessor.originConnection = originConnection;
        ExportProcessor.originDbName = originDbName;
        ExportProcessor.dataType2ClazzRuleMap = dataType2ClazzRuleMap;
    }

    public static void export() {
        for (String exportTable : exportTableList) {
            try {
                exportTable(exportTable);
            } catch (Exception e) {
                throw new Class2TableException(String.format("export table [%s] failed!", exportTable), e);
            }

        }
    }

    private static void exportTable(String exportTable) throws IOException, SQLException {
        TableMetaData tableMetaData = TableProcessor.getTableMetaData(originConnection, originDbName, exportTable);
        if (Objects.isNull(tableMetaData)) {
            throw new Class2TableException(String.format("export table [%s] failed! tableMetaData is null!", exportTable));
        }
        String classContent = ClassBuildUtil.buildClass(
                tableMetaData,
                dataType2ClazzRuleMap
        );
        save(exportTable, classContent);

    }

    private static void save(String exportTable, String classContent) throws IOException {
        File dir = new File(savePath);
        if (!dir.exists()) {
            boolean mkdirs = dir.mkdirs();
            if (!mkdirs) {
                throw new Class2TableException("export failed! mkdirs failed!");
            }
        }
        File file = new File(savePath + StrUtil.upperFirst(StrUtil.toCamelCase(exportTable)) + ".java");
        FileWriter writer = null;
        try {
            writer = new FileWriter(file);
            writer.write(classContent);
            writer.flush();
        } finally {
            if (Objects.nonNull(writer)) {
                writer.close();
            }
        }
    }
}